package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.mem.starter.entity.MemIndexEntity;
import com.jeomo.mem.starter.entity.Member;
import com.jeomo.mem.starter.mapper.MemberMapper;
import com.jeomo.mem.starter.service.IMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p>会员</p>
 *
 * @author : 崔贺
 * @date : 2020-11-18 16:22
 **/
@Service
public class MemberServiceImpl extends BaseServiceImpl<MemberMapper, Member> implements IMemberService {

    @Resource
    private MemberMapper memberMapper;


    @Override
    public List<MemIndexEntity> queryRegisterTendency(LocalDate dateFrom, LocalDate dateTo) {
        String formatFrom = dateFrom.format(DateTimeFormatter.ISO_DATE);
        if (dateFrom.equals(dateTo)) {
            List<MemIndexEntity> nullDataList = hourNullDataList();
            List<MemIndexEntity> sqlDataList = memberMapper.queryHourlyRegister(formatFrom);
            jointVo(sqlDataList, nullDataList);
            return nullDataList;
        } else {
            return queryDateList(dateFrom, dateTo);
        }
    }

    @Override
    public int queryDayCountRegister(LocalDate date) {
        return memberMapper.queryDayCountRegister(date.format(DateTimeFormatter.ISO_DATE));
    }

    @Override
    public int queryOneDayCountRegister(LocalDate date) {
        return memberMapper.queryOneDayCountRegister(date.format(DateTimeFormatter.ISO_DATE));
    }


    /**
     * 计算2个日期之间相差的  相差多少年月日
     * 比如：2011-02-02 到  2017-03-02 相差 6年，1个月，0天
     *
     * @param fromDate YYYY-MM-DD
     * @param toDate   YYYY-MM-DD
     * @return 年, 月, 日 例如 [1],[1],[1]
     */
    public List<Integer> localDateSubtract(LocalDate fromDate, LocalDate toDate) {

        Period period = Period.between(fromDate, toDate);
        List<Integer> integerList = new ArrayList<>();
        integerList.add(period.getYears());
        integerList.add(period.getMonths());
        integerList.add(period.getDays());
        return integerList;

    }

    public List<MemIndexEntity> yearNullDataList(LocalDate beginYear, LocalDate endYear) {
        int begin = beginYear.getYear();
        int end = endYear.getYear();
        List<MemIndexEntity> memIndexEntityList = new ArrayList<>();
        for (int i = begin; i <= end; i++) {
            MemIndexEntity memIndexEntity = new MemIndexEntity();
            memIndexEntity.setMemDate(i + "");
            memIndexEntity.setMemCount(0);
            memIndexEntityList.add(memIndexEntity);
        }
        MemIndexEntity memIndexEntity = new MemIndexEntity();
        memIndexEntity.setMemDate("total");
        memIndexEntity.setMemCount(0);
        memIndexEntityList.add(memIndexEntity);
        return memIndexEntityList;
    }

    public List<MemIndexEntity> hourNullDataList() {
        List<MemIndexEntity> memIndexEntityList = new ArrayList<>();
        MemIndexEntity entity = new MemIndexEntity();
        entity.setMemDate("00");
        entity.setMemCount(0);
        memIndexEntityList.add(entity);
        for (int i = 1; i <= 9; i++) {
            MemIndexEntity memIndexEntity = new MemIndexEntity();
            memIndexEntity.setMemDate("0" + i);
            memIndexEntity.setMemCount(0);
            memIndexEntityList.add(memIndexEntity);
        }
        for (int i = 0; i <= 9; i++) {
            MemIndexEntity memIndexEntity = new MemIndexEntity();
            memIndexEntity.setMemDate("1" + i);
            memIndexEntity.setMemCount(0);
            memIndexEntityList.add(memIndexEntity);
        }
        for (int i = 0; i <= 3; i++) {
            MemIndexEntity memIndexEntity = new MemIndexEntity();
            memIndexEntity.setMemDate("2" + i);
            memIndexEntity.setMemCount(0);
            memIndexEntityList.add(memIndexEntity);
        }
        return memIndexEntityList;
    }

    public List<MemIndexEntity> monthNullDataList(LocalDate begin, int month) {
        List<MemIndexEntity> memIndexEntityList = new ArrayList<>();
        for (int i = 0; i <= month; i++) {
            MemIndexEntity memIndexEntity = new MemIndexEntity();
            String date = begin.plusMonths(i).format(DateTimeFormatter.ISO_DATE);
            String substring = date.substring(0, 7);
            memIndexEntity.setMemDate(substring);
            memIndexEntity.setMemCount(0);
            memIndexEntityList.add(memIndexEntity);
        }
        MemIndexEntity memIndexEntity = new MemIndexEntity();
        memIndexEntity.setMemDate("total");
        memIndexEntity.setMemCount(0);
        memIndexEntityList.add(memIndexEntity);
        return memIndexEntityList;
    }

    public List<MemIndexEntity> dayNullDataList(LocalDate begin, int day) {
        List<MemIndexEntity> memIndexEntityList = new ArrayList<>();
        for (int i = 0; i <= day; i++) {
            MemIndexEntity memIndexEntity = new MemIndexEntity();
            String date = begin.plusDays(i).format(DateTimeFormatter.ISO_DATE);
            memIndexEntity.setMemDate(date);
            memIndexEntity.setMemCount(0);
            memIndexEntityList.add(memIndexEntity);
        }
        MemIndexEntity memIndexEntity = new MemIndexEntity();
        memIndexEntity.setMemDate("total");
        memIndexEntity.setMemCount(0);
        memIndexEntityList.add(memIndexEntity);
        return memIndexEntityList;
    }

    public static void jointVo(List<MemIndexEntity> sqlDataList, List<MemIndexEntity> voDataLists) {
        for (MemIndexEntity sql : sqlDataList) {
            for (MemIndexEntity voData : voDataLists) {
                if (sql.getMemDate().equals(voData.getMemDate())) {
                    voData.setMemCount(sql.getMemCount());
                }
            }
        }
    }

    public List<MemIndexEntity> queryDateList(LocalDate f, LocalDate t) {
        List<Integer> integerList = localDateSubtract(f, t);
        String begin = f.format(DateTimeFormatter.ISO_DATE);
        String end = t.format(DateTimeFormatter.ISO_DATE);
        //大于12个月
        Integer year = integerList.get(0);
        Integer month = integerList.get(1);
        Integer day = integerList.get(2);
        if (year > 0) {
            List<MemIndexEntity> yearNullDataList = yearNullDataList(f, t);
            List<MemIndexEntity> sqlDataList = memberMapper.queryDateRegister(begin, end, 0);
            jointVo(sqlDataList, yearNullDataList);
            return yearNullDataList;
        } else {
            //大于1个月小于12个月
            if (1 <= month && month < 12) {
                List<MemIndexEntity> monthNullDataList = monthNullDataList(f, month);
                List<MemIndexEntity> sqlDataList = memberMapper.queryDateRegister(begin, end, 1);
                jointVo(sqlDataList, monthNullDataList);
                return monthNullDataList;
            } else {
                //小于一个月
                List<MemIndexEntity> dayNullDataList = dayNullDataList(f, day);
                List<MemIndexEntity> sqlDataList = memberMapper.queryDateRegister(begin, end, 2);
                jointVo(sqlDataList, dayNullDataList);
                return dayNullDataList;
            }
        }
    }

}
